package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.dao.HAnnexDao;
import com.zhongwang.mms.module.half.model.HInventoryAndHAnnexModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.HInventoryMaterielInfoService;
import com.zhongwang.mms.module.half.service.HInventoryService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:库存查询
 * @author: th
 * @time: 2020/4/25 10:59
 */
@Controller
@RequestMapping("/half/halInvManage/inventoryQuery")
public class InventoryQueryController extends BaseController {
    //页面文件路径
    private String prefix = "half/halInvManage/inventoryQuery";
    @Autowired
    private HInventoryService hInventoryService;
    @Autowired
    private HBasicService HBasicService;
    @Autowired
    private HInventoryMaterielInfoService hInventoryMaterielInfoService;
    @Autowired
    private HAnnexDao hAnnexDao;

    @GetMapping("/page")
    @RequiresPermissions({"halinv:halinvquery:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("dept", HBasicService.getDept());
        model.addAttribute("warInfo", HBasicService.getFinishWarInfo());
        return prefix + "/list";
    }

    /**
     * 加载页面
     * @param hInventory
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"halinv:halinvquery:view"})
    public JsonResult list(HInventoryQueryModel hInventory) {

        startPage();
        return jsonResult(hInventoryService.selectInventoryByQuery(hInventory));
    }

    /**
     * 查看库存信息
     * @param imiItyId
     * @return
     */
    @ResponseBody
    @PostMapping("/view")
    @RequiresPermissions({"halinv:halinvquery:view"})
    public JsonResult view(Integer imiItyId){

        startPage();
        HInventoryAndHAnnexModel hInventoryAndHAnnexModel = new HInventoryAndHAnnexModel();
        hInventoryAndHAnnexModel.setHAnnex(hAnnexDao.selectAnnexByItyId(imiItyId));
        hInventoryAndHAnnexModel.setHInventoryMaterielInfos(hInventoryMaterielInfoService.selectAllByItyId(imiItyId));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(hInventoryAndHAnnexModel);
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        return jsonResult;
    }

    /**
     * 导出
     * @param param
     */
    @PostMapping("/exportExcel")
    @ResponseBody
    @RequiresPermissions({"halinv:halinvquery:export"})
    public JsonResult exportExcel(HInventoryQueryModel param) {
       return hInventoryService.exportExcel(param);
    }

    @GetMapping("/page/{ityMatType}")
    @RequiresPermissions({"halinv:halinvquery:view"})
    public String createId(Model model, @PathVariable("ityMatType") Integer ityMatType) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("ityMatType", ityMatType);
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("dept", HBasicService.getDept());
        model.addAttribute("warInfo", HBasicService.getWarInfo());
        return prefix + "/list";
    }
}
